<template>
  <div class="container">
    <el-card>
      <!-- 头部搜索 -->
      <el-form class="head" onsubmit="return false">
        <el-form-item class="search" :model="reqData">
          <span>学科名称</span>
          <el-input
            placeholder="请输入"
            v-model="subjectName"
            @keyup.enter.native="searchBtn()"
          >
          </el-input>
          <el-button @click="clear">清除</el-button>
          <el-button type="primary" @click="searchBtn">搜索</el-button>
        </el-form-item>
        <el-form-item class="add">
          <el-button type="success" @click="add"
            ><i class="el-icon-edit"></i>新增学科</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 警告框 -->
      <div class="hd-alert">
        <el-alert type="info" :closable="false"
          ><i class="el-icon-info"></i> 数据一共{{ counts }}条</el-alert
        >
      </div>
      <!-- table表格 -->
      <el-table
        :data="subList"
        ref="subList"
        :header-cell-style="{ background: '#f4f4f5' }"
      >
        <el-table-column label="序号" width="80">
          <template v-slot="{ $index }">
            {{ $index + 1 + (reqData.page - 1) * reqData.pagesize }}
          </template>
        </el-table-column>
        <el-table-column prop="subjectName" label="学科名称" width="180">
        </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column label="创建日期">
          <template v-slot="{ row }">
            {{ row.addDate | formatTime }}
          </template>
        </el-table-column>
        <el-table-column label="前台是否显示">
          <template v-slot="{ row }">
            {{ row.isFrontDisplay | isShow }}
          </template>
        </el-table-column>
        <el-table-column prop="twoLevelDirectory" label="二级目录">
        </el-table-column>
        <el-table-column prop="tags" label="标签"> </el-table-column>
        <el-table-column prop="totals" label="题目数量"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              type="text"
              size="small"
              @click="
                $router.push(
                  `/subjects/directorys?id=${row.id}&name=${row.subjectName}`
                )
              "
              >学科分类</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="
                $router.push(
                  `/subjects/tags?id=${row.id}&name=${row.subjectName}`
                )
              "
              >学科标签</el-button
            >
            <el-button type="text" size="small" @click="edit(row)"
              >修改</el-button
            >
            <el-button type="text" size="small" @click="del(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="reqData.page"
          :page-sizes="[1, 10, 20, 30, 40]"
          :page-size="reqData.pagesize"
          layout="prev, pager, next, sizes, jumper"
          :total="counts"
        >
        </el-pagination>
      </div>
    </el-card>
    <!-- 弹框 -->
    <el-dialog
      :title="{ add: '新增学科', edit: '修改学科' }[mode]"
      :visible.sync="dialogVisible"
      width="21%"
      :before-close="close"
    >
      <!-- 表单验证 -->
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="学科名称" prop="subjectName">
          <el-input v-model="form.subjectName"></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch
            v-model="value"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
          >
            >
          </el-switch>
        </el-form-item>
      </el-form>
      <!-- 表单验证 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list, add, remove, update, simple } from '../../api/hmmm/subjects'
import dayjs from 'dayjs'
export default {
  data () {
    return {
      subList: [],
      counts: 0,
      mode: 'add',
      dialogVisible: false,
      value: '1',
      form: {
        id: '',
        subjectName: '',
        isFrontDisplay: ''
      },
      rules: {
        subjectName: [
          { required: true, message: '请输入相关内容', trigger: 'change' }
        ]
      },
      reqData: {
        page: 1,
        pagesize: 10
      },
      subjectName: ''
    }
  },
  created () {
    this.getData()
  },
  methods: {
    // 获取学科列表
    async getData () {
      const { data } = await list(this.reqData)
      // console.log(data)
      this.counts = data.counts
      this.subList = data.items
    },
    // 清楚搜索框
    clear () {
      this.subjectName = ''
    },
    //  点击搜索按钮
    async searchBtn () {
      this.reqData = { ...this.reqData, subjectName: this.subjectName }
      this.getData()
    },
    // 关闭弹框
    close () {
      this.dialogVisible = false
      this.$refs.form.resetFields()
      this.form = {
        id: '',
        subjectName: '',
        isFrontDisplay: ''
      }
    },
    // 点击确定提交数据
    async submit () {
      this.form.isFrontDisplay = this.value
      // 添加学科的确定按钮
      if (this.mode === 'add') {
        await add(this.form)
        this.$message.success('添加学科成功')
        this.getData()
      }
      // 修改学科的提交按钮
      if (this.mode === 'edit') {
        await update(this.form)
        this.$message.success('修改学科成功')
        this.getData()
      }
      this.close()
    },
    // 添加学科
    add () {
      this.mode = 'add'
      this.dialogVisible = true
    },
    // 修改学科
    edit (row) {
      // console.log(row)
      this.mode = 'edit'
      this.dialogVisible = true
      if (this.mode === 'edit') {
        this.form.subjectName = row.subjectName
        this.form.id = row.id
      }
    },
    // 删除学科
    del (row) {
      // console.log(row)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示')
        .then(async () => {
          await remove(row)
          this.$message.success('删除成功')
          this.getData()
        })
        .catch(() => {})
      // 捕获错误
    },
    // 点击页码
    handleCurrentChange (val) {
      // console.log(val)
      this.reqData.page = val
      this.getData()
    },
    // 点击页容量
    handleSizeChange (val) {
      // console.log(val)
      this.reqData.pagesize = val
      this.reqData.page = 1
      this.getData()
    }
  },
  // 时间格式化
  filters: {
    formatTime (time) {
      // console.log(time)
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    },
    isShow (val) {
      if (val === 1) {
        return '是'
      }
      if (val === 0) {
        return '否'
      }
    }
  }
}
</script>

<style scoped lang="scss">
.container {
  .el-card {
    .head {
      display: flex;
      justify-content: space-between;
      .search {
        font-weight: 800;
        .el-input {
          margin: 0 15px;
          width: 200px;
        }
      }
    }
    .hd-alert {
      margin: 10px 0;
      .el-alert {
        padding: 0;
        height: 35px;
        font-size: 16px;
        .el-icon-info {
          font-size: 16px;
        }
      }
    }
    .el-table {
      margin: 15px 0;
    }
    .pagination {
      float: right;
      margin: 10px 0;
    }
  }
}
</style>
